﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI.HtmlControls;

namespace System.Web.UI
{
    public class ZMPageSNEdit<T, TSModel, TNModel> : ZMPageEdit<TSModel>
        where T : SNModel<TSModel, TNModel>, new()
        where TSModel : BaseModel, new()
        where TNModel : BaseModel, new()
    {
        protected virtual T DataModel { get; set; }

        private HtmlInputHidden baseSubDeleteIds;
        private HtmlInputHidden baseSubListDatas;


        protected virtual HtmlInputHidden BaseSubDeleteIds
        {
            get
            {
                RegSubDeleteIds();
                return baseSubDeleteIds;
            }
        }
        protected virtual HtmlInputHidden BaseSubListDatas
        {
            get
            {
                RegSubListDatas();
                return baseSubListDatas;
            }
        }

        protected virtual void RegSubDeleteIds()
        {
            if (baseSubDeleteIds == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseSubDeleteIds = (HtmlInputHidden)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("subDeleteIDs");
                            break;
                        case 2:
                            baseSubDeleteIds = (HtmlInputHidden)Page.Master.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("subDeleteIDs");
                            break;
                        default:
                            baseSubDeleteIds = (HtmlInputHidden)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("subDeleteIDs");
                            break;
                    }
                }
                else
                    baseSubDeleteIds = Page.FindControl("subDeleteIDs") as HtmlInputHidden;
            }
        }
        protected virtual void RegSubListDatas()
        {
            if (baseSubListDatas == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseSubListDatas = (HtmlInputHidden)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("subListDatas");
                            break;
                        case 2:
                            baseSubListDatas = (HtmlInputHidden)Page.Master.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("subListDatas");
                            break;
                        default:
                            baseSubListDatas = (HtmlInputHidden)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("subListDatas");
                            break;
                    }
                }
                else
                    baseSubListDatas = Page.FindControl("subDeleteIDs") as HtmlInputHidden;
            }
        }      
        protected override void RegBottomBar()
        {
            if (baseBottomBar == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseBottomBar = (Control)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("bottomBar");
                            break;
                        case 2:
                            baseBottomBar = (Control)Page.Master.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("bottomBar");
                            break;
                        default:
                            baseBottomBar = (Control)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("bottomBar");
                            break;
                    }
                }
                else
                    baseBottomBar = Page.FindControl("bottomBar") as Control;
            }
        }       
        protected override void RegEditContainer()
        {
            if (baseEditContainer == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseEditContainer = (Control)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder");
                            break;
                        case 2:
                            baseEditContainer = (Control)Page.Master.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder");
                            break;
                        default:
                            baseEditContainer = (Control)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder");
                            break;
                    }
                }
                else
                    baseEditContainer = Page as Control;
            }
        }

        protected override void InitModel()
        {
            base.InitModel();
            DataModel = new T();
            DataModel.Model = Model;
            if (!IsNewModel)
            {
                BaseBLL<TNModel> bll = new BaseBLL<TNModel>();
                DataModel.NModles = bll.GetList("", DataModel.NRelationFieldName + "='" + CModel.GetProValue(DataModel.Model, DataModel.Model.GetPrimaryKey()) + "'", "", null);
                JavaScriptSerializer js = new JavaScriptSerializer();
                DataModel.NSubListJsonData = js.Serialize(DataModel.NModles);
            }
        }

        protected override void OnAchieve()
        {
            base.OnAchieve();
            OnAchieveSubModel();
        }
        protected virtual void OnAchieveSubModel()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            if (!string.IsNullOrEmpty(BaseSubListDatas.Value))
            {
                List<TNModel> subModels = js.Deserialize<List<TNModel>>(BaseSubListDatas.Value);
                foreach (TNModel model in subModels)
                {
                    OnAchieveSubModel(model);
                    CModel.SetProValue(model, new T().NRelationFieldName, CModel.GetProValue(Model, Model.GetPrimaryKey()));
                }
                if (DataModel == null)
                    DataModel = new T();
                DataModel.Model = Model;
                DataModel.NModles = subModels;
            }
        }
        protected virtual void OnAchieveSubModel(TNModel model)
        {

        }
        protected override void OnSave()
        {
            base.OnSave();
            BaseBLL<TNModel> bll = new BaseBLL<TNModel>();
            bll.InsertOrUpdateBySQL(DataModel.NModles);
            if (!string.IsNullOrEmpty(BaseSubDeleteIds.Value))
            {
                string ids = "('" + BaseSubDeleteIds.Value.Trim(',').Trim().Replace(",", "','") + "')";
                bll.Delete(new TNModel().GetPrimaryKey() + " IN " + ids, new List<DBParameter>());
            }
        }
    }
}